import { ref, computed } from 'vue'
import { defineStore } from 'pinia'

export const useMusicStore = defineStore('music', () => {
  // 用户是否为会员
  const isVip = ref(false)
  
  // 模拟的歌曲列表数据
  const playlist = ref([
    {
      id: 1,
      title: '海阔天空',
      artist: 'Beyond',
      cover: 'https://picsum.photos/seed/music1/300/300',
      url: 'https://samplelib.com/lib/preview/mp3/sample-15s.mp3', // 替换为稳定的测试音频
      isPremium: false, // 非付费歌曲
      lyrics: [
        { time: 0, text: '海阔天空 - Beyond' },
        { time: 2, text: '词：黄家驹' },
        { time: 4, text: '曲：黄家驹' },
        { time: 8, text: '今天我寒夜里看雪飘过' },
        { time: 12, text: '怀着冷却了的心窝漂远方' },
        { time: 16, text: '风雨里追赶 雾里分不清影踪' },
        { time: 21, text: '天空海阔你与我' },
        { time: 24, text: '可会变（谁没在变）' },
        { time: 28, text: '多少次迎着冷眼与嘲笑' },
        { time: 32, text: '从没有放弃过心中的理想' },
        { time: 37, text: '一刹那恍惚 若有所失的感觉' },
        { time: 41, text: '不知不觉已变淡' },
        { time: 44, text: '心里爱（谁明白我）' },
        { time: 48, text: '原谅我这一生不羁放纵爱自由' },
        { time: 53, text: '也会怕有一天会跌倒' },
        { time: 57, text: '背弃了理想 谁人都可以' },
        { time: 61, text: '哪会怕有一天只你共我' }
      ]
    },
    {
      id: 2,
      title: '夜曲',
      artist: '周杰伦',
      cover: 'https://picsum.photos/seed/music2/300/300',
      url: 'https://samplelib.com/lib/preview/mp3/sample-3s.mp3', // 替换为稳定的测试音频
      isPremium: true, // 付费歌曲
      lyrics: [
        { time: 0, text: '夜曲 - 周杰伦' },
        { time: 2, text: '词：方文山' },
        { time: 4, text: '曲：周杰伦' },
        { time: 8, text: '一群嗜血的蚂蚁 被腐肉所吸引' },
        { time: 12, text: '我面无表情 看孤独的风景' },
        { time: 16, text: '失去你 爱恨开始分明' },
        { time: 20, text: '失去你 还有什么事好关心' },
        { time: 24, text: '当鸽子不再象征和平' },
        { time: 28, text: '我终于被提醒 广场上喂食的是秃鹰' },
        { time: 33, text: '我用漂亮的押韵 形容被掠夺一空的爱情' },
        { time: 37, text: '啊 乌云开始遮蔽 夜色不干净' },
        { time: 41, text: '公园里 葬礼的回音 在漫天飞行' },
        { time: 46, text: '送你的白色玫瑰 在纯黑的环境凋零' },
        { time: 50, text: '乌鸦在树枝上诡异的很安静' },
        { time: 55, text: '静静听 我黑色的大衣 想温暖你' },
        { time: 59, text: '日渐冰冷的回忆 走过的走过的生命' }
      ]
    },
    {
      id: 3,
      title: '光年之外',
      artist: '邓紫棋',
      cover: 'https://picsum.photos/seed/music3/300/300',
      url: 'https://samplelib.com/lib/preview/mp3/sample-6s.mp3', // 替换为稳定的测试音频
      isPremium: true, // 付费歌曲
      lyrics: [
        { time: 0, text: '光年之外 - 邓紫棋' },
        { time: 2, text: '词：邓紫棋' },
        { time: 4, text: '曲：邓紫棋' },
        { time: 8, text: '感受停在我发端的指尖' },
        { time: 12, text: '如何瞬间 冻结时间' },
        { time: 16, text: '记住望着我坚定的双眼' },
        { time: 20, text: '也许已经 没有明天' },
        { time: 25, text: '面对浩瀚的星海' },
        { time: 29, text: '我们微小得像尘埃' },
        { time: 33, text: '漂浮在 一片无奈' },
        { time: 37, text: '缘分让我们相遇乱世以外' },
        { time: 42, text: '命运却要我们危难中相爱' },
        { time: 46, text: '也许未来遥远在光年之外' },
        { time: 51, text: '我愿守候未知里为你等待' },
        { time: 55, text: '我没想到 为了你 我能疯狂到' },
        { time: 60, text: '山崩海啸 没有你 根本不想逃' },
        { time: 64, text: '我的大脑 为了你 已经疯狂到' },
        { time: 69, text: '脉搏心跳 没有你 根本不重要' }
      ]
    },
    {
      id: 4,
      title: '成都',
      artist: '赵雷',
      cover: 'https://picsum.photos/seed/music4/300/300',
      url: 'https://samplelib.com/lib/preview/mp3/sample-9s.mp3', // 替换为稳定的测试音频
      isPremium: false, // 非付费歌曲
      lyrics: [
        { time: 0, text: '成都 - 赵雷' },
        { time: 2, text: '词：赵雷' },
        { time: 4, text: '曲：赵雷' },
        { time: 8, text: '让我掉下眼泪的 不止昨夜的酒' },
        { time: 12, text: '让我依依不舍的 不止你的温柔' },
        { time: 17, text: '余路还要走多久 你攥着我的手' },
        { time: 21, text: '让我感到为难的 是挣扎的自由' },
        { time: 26, text: '分别总是在九月 回忆是思念的愁' },
        { time: 30, text: '深秋嫩绿的垂柳 亲吻着我额头' },
        { time: 35, text: '在那座阴雨的小城里 我从未忘记你' },
        { time: 40, text: '成都 带不走的 只有你' },
        { time: 45, text: '和我在成都的街头走一走 喔哦...' },
        { time: 50, text: '直到所有的灯都熄灭了也不停留' },
        { time: 55, text: '你会挽着我的衣袖 我会把手揣进裤兜' },
        { time: 60, text: '走到玉林路的尽头 坐在小酒馆的门口' }
      ]
    },
    {
      id: 5,
      title: '起风了',
      artist: '买辣椒也用券',
      cover: 'https://picsum.photos/seed/music5/300/300',
      url: 'https://www.soundhelix.com/examples/mp3/SoundHelix-Song-5.mp3', // 示例音频文件
      isPremium: false, // 非付费歌曲
      lyrics: [
        { time: 0, text: '起风了 - 买辣椒也用券' },
        { time: 2, text: '词：米果' },
        { time: 4, text: '曲：高桥优' },
        { time: 8, text: '这一路上走走停停 顺着少年漂流的痕迹' },
        { time: 13, text: '迈出车站的前一刻 忽然不知要去哪里' },
        { time: 17, text: '看着周围的人群 充满着陌生的气息' },
        { time: 22, text: '也许期待的不过是 与时间为敌' },
        { time: 26, text: '再次看到你 微凉晨光里 笑得很甜蜜' },
        { time: 31, text: '从前初识这世间 万般流连 看着天边似在眼前' },
        { time: 36, text: '也甘愿赴汤蹈火去走它一遍' },
        { time: 40, text: '如今走过这世间 万般流连 翻过岁月不同侧脸' },
        { time: 45, text: '措不及防闯入你的笑颜' },
        { time: 49, text: '我曾难自拔于世界之大 也沉溺于其中梦话' },
        { time: 54, text: '不得真假 不做挣扎 不惧笑话' },
        { time: 58, text: '我曾将青春翻涌成她 也曾指尖弹出盛夏' },
        { time: 63, text: '心之所动 且就随缘去吧' }
      ]
    }
  ])
  
  // 切换用户会员状态（实际项目中应该通过API验证）
  function toggleVip() {
    isVip.value = !isVip.value
  }

  // 热门歌手数据
  const topSingers = ref([
    {
      id: 101,
      name: '周杰伦',
      avatar: 'https://picsum.photos/seed/singer1/200/200',
      songCount: 320,
      fansCount: 35600000,
      rank: 1
    },
    {
      id: 102,
      name: '陈奕迅',
      avatar: 'https://picsum.photos/seed/singer2/200/200',
      songCount: 280,
      fansCount: 28900000,
      rank: 2
    },
    {
      id: 103,
      name: 'Taylor Swift',
      avatar: 'https://picsum.photos/seed/singer3/200/200',
      songCount: 250,
      fansCount: 42300000,
      rank: 3
    },
    {
      id: 104,
      name: '邓紫棋',
      avatar: 'https://picsum.photos/seed/singer4/200/200',
      songCount: 180,
      fansCount: 21500000,
      rank: 4
    },
    {
      id: 105,
      name: '林俊杰',
      avatar: 'https://picsum.photos/seed/singer5/200/200',
      songCount: 260,
      fansCount: 29800000,
      rank: 5
    },
    {
      id: 106,
      name: '张学友',
      avatar: 'https://picsum.photos/seed/singer6/200/200',
      songCount: 420,
      fansCount: 26700000,
      rank: 6
    },
    {
      id: 107,
      name: '李荣浩',
      avatar: 'https://picsum.photos/seed/singer7/200/200',
      songCount: 120,
      fansCount: 18900000,
      rank: 7
    },
    {
      id: 108,
      name: 'Billie Eilish',
      avatar: 'https://picsum.photos/seed/singer8/200/200',
      songCount: 100,
      fansCount: 31200000,
      rank: 8
    },
    {
      id: 109,
      name: 'Ed Sheeran',
      avatar: 'https://picsum.photos/seed/singer9/200/200',
      songCount: 190,
      fansCount: 38500000,
      rank: 9
    },
    {
      id: 110,
      name: '周杰伦',
      avatar: 'https://picsum.photos/seed/singer10/200/200',
      songCount: 240,
      fansCount: 23400000,
      rank: 10
    }
  ])

  // 热门曲目数据
  const topSongs = ref([
    {
      id: 201,
      title: '孤勇者',
      artist: '陈奕迅',
      cover: 'https://picsum.photos/seed/topsong1/300/300',
      playCount: 125000000,
      rank: 1,
      url: 'https://www.soundhelix.com/examples/mp3/SoundHelix-Song-6.mp3' // 示例音频文件
    },
    {
      id: 202,
      title: '起风了',
      artist: '买辣椒也用券',
      cover: 'https://picsum.photos/seed/topsong2/300/300',
      playCount: 112000000,
      rank: 2,
      url: 'https://www.soundhelix.com/examples/mp3/SoundHelix-Song-7.mp3' // 示例音频文件
    },
    {
      id: 203,
      title: '晴天',
      artist: '周杰伦',
      cover: 'https://picsum.photos/seed/topsong3/300/300',
      playCount: 98000000,
      rank: 3,
      url: 'https://www.soundhelix.com/examples/mp3/SoundHelix-Song-8.mp3' // 示例音频文件
    },
    {
      id: 204,
      title: '光年之外',
      artist: '邓紫棋',
      cover: 'https://picsum.photos/seed/topsong4/300/300',
      playCount: 89000000,
      rank: 4,
      url: 'https://www.soundhelix.com/examples/mp3/SoundHelix-Song-9.mp3' // 示例音频文件
    },
    {
      id: 205,
      title: '海阔天空',
      artist: 'Beyond',
      cover: 'https://picsum.photos/seed/topsong5/300/300',
      playCount: 85000000,
      rank: 5,
      url: 'https://www.soundhelix.com/examples/mp3/SoundHelix-Song-10.mp3' // 示例音频文件
    },
    {
      id: 206,
      title: '成都',
      artist: '赵雷',
      cover: 'https://picsum.photos/seed/topsong6/300/300',
      playCount: 78000000,
      rank: 6,
      url: 'https://www.soundhelix.com/examples/mp3/SoundHelix-Song-11.mp3' // 示例音频文件
    },
    {
      id: 207,
      title: '可惜没如果',
      artist: '林俊杰',
      cover: 'https://picsum.photos/seed/topsong7/300/300',
      playCount: 72000000,
      rank: 7,
      url: 'https://www.soundhelix.com/examples/mp3/SoundHelix-Song-12.mp3' // 示例音频文件
    },
    {
      id: 208,
      title: '告白气球',
      artist: '周杰伦',
      cover: 'https://picsum.photos/seed/topsong8/300/300',
      playCount: 69000000,
      rank: 8,
      url: 'https://www.soundhelix.com/examples/mp3/SoundHelix-Song-13.mp3' // 示例音频文件
    },
    {
      id: 209,
      title: '像鱼',
      artist: '王贰浪',
      cover: 'https://picsum.photos/seed/topsong9/300/300',
      playCount: 65000000,
      rank: 9,
      url: 'https://www.soundhelix.com/examples/mp3/SoundHelix-Song-14.mp3' // 示例音频文件
    },
    {
      id: 210,
      title: '你的答案',
      artist: '阿冗',
      cover: 'https://picsum.photos/seed/topsong10/300/300',
      playCount: 62000000,
      rank: 10,
      url: 'https://www.soundhelix.com/examples/mp3/SoundHelix-Song-15.mp3' // 示例音频文件
    }
  ])

  // 当前正在播放的歌曲索引
  const currentIndex = ref(0)
  
  // 播放状态
  const isPlaying = ref(false)
  
  // 音量
  const volume = ref(0.8)
  
  // 是否静音
  const isMuted = ref(false)
  
  // 当前播放时间
  const currentTime = ref(0)
  
  // 歌曲总时长
  const duration = ref(0)
  
  // 计算属性：当前播放的歌曲
  const currentSong = computed(() => {
    return playlist.value[currentIndex.value] || {
      title: '暂无歌曲',
      artist: '未知艺术家',
      cover: 'https://picsum.photos/seed/default/300/300',
      url: ''
    }
  })
  
  // 方法：切换播放/暂停状态
  function togglePlay() {
    isPlaying.value = !isPlaying.value
  }
  
  // 方法：播放上一首
  function prevSong() {
    currentIndex.value = (currentIndex.value - 1 + playlist.value.length) % playlist.value.length
    currentTime.value = 0
  }
  
  // 方法：播放下一首
  function nextSong() {
    currentIndex.value = (currentIndex.value + 1) % playlist.value.length
    currentTime.value = 0
  }
  
  // 方法：设置音量
  function setVolume(newVolume) {
    volume.value = newVolume
  }
  
  // 方法：切换静音状态
  function toggleMute() {
    isMuted.value = !isMuted.value
  }
  
  // 方法：设置当前播放时间
  function setCurrentTime(newTime) {
    currentTime.value = newTime
  }
  
  // 方法：设置歌曲总时长
  function setDuration(newDuration) {
    duration.value = newDuration
  }
  
  // 方法：添加歌曲到播放列表
  function addSong(song) {
    playlist.value.push(song)
  }
  
  // 方法：从播放列表中移除歌曲
  function removeSong(songId) {
    const index = playlist.value.findIndex(song => song.id === songId)
    if (index !== -1) {
      playlist.value.splice(index, 1)
      if (currentIndex.value >= index && currentIndex.value > 0) {
        currentIndex.value--
      } else if (playlist.value.length === 0) {
        currentIndex.value = 0
      }
    }
  }
  
  // 方法：清空播放列表
  function clearPlaylist() {
    playlist.value = []
    currentIndex.value = 0
    currentTime.value = 0
    isPlaying.value = false
  }
  
  // 返回store的状态和方法
  return {
    playlist,
    currentIndex,
    isPlaying,
    volume,
    isMuted,
    currentTime,
    duration,
    currentSong,
    topSingers,
    topSongs,
    isVip,
    togglePlay,
    prevSong,
    nextSong,
    setVolume,
    toggleMute,
    setCurrentTime,
    setDuration,
    addSong,
    removeSong,
    clearPlaylist,
    toggleVip
  }
})